# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os,time
import pymysql.cursors

class DoubanmoviePipeline(object):

    def process_item(self, item, spider):
        #将数据存入mysql数据库
        connect = pymysql.connect(
            host='localhost',  # 服务器的IP地址 本地的填localhost
            port=3306,  # mysql端口 一般为3306
            user='root',  # 账号
            passwd='xxxxxxx',  # 你的密码
            db='douban_spider',  # 数据库名称
            charset='utf8'
        )

        st = item['Mov_abstract'][0].lstrip().split('主')
        drank = item['Mov_Rank'][0]
        dname = %item['Mov_Name'][0]
        dgrade = item['Mov_grade'][0]
        discribe = item['Mov_discribe'][0]
        director = st[0].lstrip('导演:')
        starring = st[1].lstrip('演:')
        time = item['Mov_abstract'][1].lstrip().split('/')[0]
        country = item['Mov_abstract'][1].lstrip().split('/')[1]
        type = item['Mov_abstract'][1].lstrip().split('/')[2]
        pic = item['Mov_pic'][0]

        #拼接字符串
        data = "insert into movieTop250 values("+'\"'+drank+'\"'+","+'\"'+dname+'\"'+","+'\"'+dgrade+'\"'+","+'\"'+discribe+'\"'+","+'\"'+director+'\"'+","+'\"'+starring+'\"'+","+'\"'+time+'\"'+","+'\"'+country+'\"'+","+'\"'+type+'\"'+","+'\"'+pic+'\"'+")"
        #获取游标
        cursor = connect.cursor()
        # 插入数据
        cursor.execute(data)
        #提交事物
        connect.commit()
        #关闭游标
        cursor.close()
        #关闭链接
        connect.close()

        return item
